package com.edu.system.service.impl;

import java.util.List;
import java.util.Optional;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.system.mapper.TClassExtMapper;
import com.edu.system.domain.TClassExt;
import com.edu.system.service.ITClassExtService;

/**
 * 班级(智慧校园)Service业务层处理
 * 
 * @author xxxx
 * @date 2024-02-01
 */
@Service
public class TClassExtServiceImpl implements ITClassExtService 
{
    @Autowired
    private TClassExtMapper tClassExtMapper;

    /**
     * 查询班级(智慧校园)
     * 
     * @param id 班级(智慧校园)主键
     * @return 班级(智慧校园)
     */
    @Override
    public TClassExt selectTClassExtById(Long id)
    {
        return tClassExtMapper.selectTClassExtById(id);
    }

    /**
     * 查询班级(智慧校园)列表
     * 
     * @param tClassExt 班级(智慧校园)
     * @return 班级(智慧校园)
     */
    @Override
    public List<TClassExt> selectTClassExtList(TClassExt tClassExt)
    {
        return tClassExtMapper.selectTClassExtList(tClassExt);
    }

    @Override
    public TClassExt checkAndSaveByCode(TClassExt classExt) {
        List<TClassExt> tClassExts = tClassExtMapper.selectTClassExtList(
            TClassExt.builder().code(classExt.getCode()).build()
        );

        return Optional.ofNullable(tClassExts)
            .filter(CollectionUtils::isNotEmpty)
            .map(a -> a.get(0))
            .map(a -> {
                if (classExt.getLocationId() != null){
                    TClassExt ext = new TClassExt();
                    ext.setId(a.getId());
                    ext.setLocationId(classExt.getLocationId());
                    ext.setLocationName(classExt.getLocationName());
                    tClassExtMapper.updateTClassExt(ext);
                }
                return a;
            })
            .orElseGet(() -> {
                classExt.setId(null);
                this.insertTClassExt(classExt);
                return classExt;
            });
    }

    /**
     * 新增班级(智慧校园)
     * 
     * @param tClassExt 班级(智慧校园)
     * @return 结果
     */
    @Override
    public int insertTClassExt(TClassExt tClassExt)
    {
        return tClassExtMapper.insertTClassExt(tClassExt);
    }

    /**
     * 修改班级(智慧校园)
     * 
     * @param tClassExt 班级(智慧校园)
     * @return 结果
     */
    @Override
    public int updateTClassExt(TClassExt tClassExt)
    {
        return tClassExtMapper.updateTClassExt(tClassExt);
    }

    /**
     * 批量删除班级(智慧校园)
     * 
     * @param ids 需要删除的班级(智慧校园)主键
     * @return 结果
     */
    @Override
    public int deleteTClassExtByIds(Long[] ids)
    {
        return tClassExtMapper.deleteTClassExtByIds(ids);
    }

    /**
     * 删除班级(智慧校园)信息
     * 
     * @param id 班级(智慧校园)主键
     * @return 结果
     */
    @Override
    public int deleteTClassExtById(Long id)
    {
        return tClassExtMapper.deleteTClassExtById(id);
    }
}
